home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 17 / CU Amiga Magazine's Super CD-ROM 17 (1997)(EMAP Images)(GB)[!][issue 1997-12].iso / CUCD / Programming / DiceSource / src / dutil / save / dsearch.c < prev    next >
C/C++ Source or Header  |  1994-01-30  |  3KB  |  167 lines

  1.  
  2. /*
  3.  *  DSEARCH.C
  4.  *
  5.  * (c)Copyright 1992 Obvious Implementations Corp, All Rights Reserved
  6.  *
  7.  *  DSEARCH string files ...
  8.  */
  9.  
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <string.h>
  13. #include <ctype.h>
  14. #include <lib/version.h>
  15.  
  16. #ifdef _DCC
  17. IDENT("dsearch", ".3");
  18. DCOPYRIGHT;
  19. #endif
  20.  
  21. static char Tmp[256];
  22. static long LastLine = -1;    /*  offset of base of last printed line */
  23.  
  24. short QuietOpt = 0;
  25.  
  26. void look(char *, char *, FILE *);
  27. void print_line(FILE *, long, char *, char *, char *);
  28.  
  29. main(ac, av)
  30. int   ac;
  31. char **av;
  32. {
  33.     int i;
  34.  
  35.     if (ac == 1) {
  36.     puts(Ident);
  37.     puts("DSEARCH srch-string files");
  38.     puts("can use AmigaDOS wildcards for files");
  39.     exit(1);
  40.     }
  41.     {
  42.     char *ptr;
  43.  
  44.     if ((ptr = strrchr(av[0], '/')) == NULL && (ptr = strrchr(av[0], ':')) == NULL)
  45.         ptr = av[0];
  46.     else
  47.         ++ptr;
  48.     }
  49.  
  50. #ifndef unix
  51.     expand_args(ac, av, &ac, &av);
  52. #endif
  53.  
  54.     for (i = 2; i < ac; ++i) {
  55.     FILE *fi;
  56.  
  57.     if (fi = fopen(av[i], "r")) {
  58.         if (ac > 2 && !QuietOpt)
  59.         printf("--- %s ---\n", av[i]);
  60.         LastLine = -1;
  61.         look(av[1], av[i], fi);
  62.         fclose(fi);
  63.     } else {
  64.         if (QuietOpt == 0)
  65.         printf ("--- %s --- (unable to open)\n", av[i]);
  66.     }
  67.     }
  68.     return(0);
  69. }
  70.  
  71. char Buf[8192];
  72.  
  73. void
  74. look(str, fileName, fi)
  75. char *str;
  76. char *fileName;
  77. FILE *fi;
  78. {
  79.     int len = strlen(str);
  80.     long off = 0;
  81.     short n;
  82.     short nn;
  83.     char *ptr;
  84.  
  85.     while ((nn = fread(Buf, 1, sizeof(Buf), fi)) > 0) {
  86.     n = nn;
  87.     for (ptr = Buf; n > 0; ++ptr, --n) {
  88.         if (((*str ^ *ptr) & ~0x20) == 0) {
  89.         if (n < len) {
  90.             if (strnicmp(ptr, str, n) == 0) {    /*  doesn't happen */
  91.             fseek(fi, off + nn - n, 0);    /*  often, I hope. */
  92.             break;
  93.             }
  94.         } else {
  95.             if (strnicmp(ptr, str, len) == 0) {
  96.             if (QuietOpt && LastLine == -1)
  97.                 printf("--- %s ---\n", fileName);
  98.  
  99.             print_line(fi, off, Buf, ptr, Buf + nn);
  100.             }
  101.         }
  102.         }
  103.     }
  104.     off = ftell(fi);
  105.     }
  106. }
  107.  
  108. void
  109. print_line(fi, off, base, ptr, endptr)
  110. FILE *fi;
  111. long off;
  112. char *base;
  113. char *ptr;
  114. char *endptr;
  115. {
  116.     long savpos = ftell(fi);
  117.     short sook = 0;            /*    nobody said I 'aught to use good 'nglish!   */
  118.  
  119.     /*
  120.      *    search backwards for newline
  121.      */
  122.  
  123.     while (*ptr != '\n') {
  124.     if (ptr <= base) {        /*    doesn't happen often */
  125.         if (off == 0)        /*    beginning of file */
  126.         break;
  127.         off -= sizeof(Tmp);
  128.         if (off < 0)
  129.         off = 0;
  130.         fseek(fi, off, 0);
  131.         sook = 1;
  132.         base = Tmp;
  133.         endptr = base + fread(Tmp, 1, sizeof(Tmp), fi);
  134.         ptr = endptr;
  135.     }
  136.     --ptr;
  137.     }
  138.     if (LastLine != off + (endptr - ptr)) {
  139.     LastLine = off + (endptr - ptr);
  140.  
  141.        /*
  142.     *  Search forwards for newline or EOF
  143.     */
  144.  
  145.     do {
  146.         ++ptr;
  147.         if (ptr >= endptr) {    /*    doesn't happen often */
  148.         off += endptr - base;
  149.         fseek(fi, off, 0);
  150.         sook = 1;
  151.         base = Tmp;
  152.         endptr = base + fread(Tmp, 1, sizeof(Tmp), fi);
  153.         ptr = Tmp;
  154.         if (endptr <= base) {
  155.             putchar('\n');
  156.             break;
  157.         }
  158.         }
  159.         putchar(*ptr);
  160.     } while (*ptr != '\n');
  161.     }
  162.  
  163.     if (sook)
  164.     fseek(fi, savpos, 0);
  165. }
  166.  
  167.